#pragma once
#include "Header.h"

#pragma warning(disable : 4201)

//0x8 bytes (sizeof)
struct _EX_PUSH_LOCK
{
    union
    {
        struct
        {
            ULONGLONG Locked : 1;                                             //0x0
            ULONGLONG Waiting : 1;                                            //0x0
            ULONGLONG Waking : 1;                                             //0x0
            ULONGLONG MultipleShared : 1;                                     //0x0
            ULONGLONG Shared : 60;                                            //0x0
        };
        ULONGLONG Value;                                                    //0x0
        VOID* Ptr;                                                          //0x0
    };
};

//0xa0 bytes (sizeof)
struct _HANDLE_TRACE_DB_ENTRY
{
    struct _CLIENT_ID ClientId;                                             //0x0
    VOID* Handle;                                                           //0x10
    ULONG Type;                                                             //0x18
    VOID* StackTrace[16];                                                   //0x20
}; 

//0xf0 bytes (sizeof)
struct _HANDLE_TRACE_DEBUG_INFO
{
    LONG RefCount;                                                          //0x0
    ULONG TableSize;                                                        //0x4
    ULONG BitMaskFlags;                                                     //0x8
    struct _FAST_MUTEX CloseCompactionLock;                                 //0x10
    ULONG CurrentStackIndex;                                                //0x48
    struct _HANDLE_TRACE_DB_ENTRY TraceDb[1];                               //0x50
}; 

//0x68 bytes (sizeof)
typedef struct _HANDLE_TABLE
{
    ULONGLONG TableCode;                                                    //0x0
    struct _EPROCESS* QuotaProcess;                                         //0x8
    VOID* UniqueProcessId;                                                  //0x10
    struct _EX_PUSH_LOCK HandleLock;                                        //0x18
    struct _LIST_ENTRY HandleTableList;                                     //0x20
    struct _EX_PUSH_LOCK HandleContentionEvent;                             //0x30
    struct _HANDLE_TRACE_DEBUG_INFO* DebugInfo;                             //0x38
    LONG ExtraInfoPages;                                                    //0x40
    union
    {
        ULONG Flags;                                                        //0x44
        UCHAR StrictFIFO : 1;                                                 //0x44
    };
    ULONG FirstFreeHandle;                                                  //0x48
    struct _HANDLE_TABLE_ENTRY* LastFreeHandleEntry;                        //0x50
    ULONG HandleCount;                                                      //0x58
    ULONG NextHandleNeedingPool;                                            //0x5c
    ULONG HandleCountHighWatermark;                                         //0x60
}HANDLE_TABLE, * PHANDLE_TABLE;

//0x4 bytes (sizeof)
struct _HANDLE_TABLE_ENTRY_INFO
{
    ULONG AuditMask;                                                        //0x0
};

//0x10 bytes (sizeof)
typedef struct _HANDLE_TABLE_ENTRY
{
    union
    {
        VOID* Object;                                                       //0x0
        ULONG ObAttributes;                                                 //0x0
        struct _HANDLE_TABLE_ENTRY_INFO* InfoTable;                         //0x0
        ULONGLONG Value;                                                    //0x0
    };
    union
    {
        ULONG GrantedAccess;                                                //0x8
        struct
        {
            USHORT GrantedAccessIndex;                                      //0x8
            USHORT CreatorBackTraceIndex;                                   //0xa
        };
        ULONG NextFreeTableEntry;                                           //0x8
    };
}HANDLE_TABLE_ENTRY, * PHANDLE_TABLE_ENTRY;

//0x40 bytes (sizeof)
struct _HANDLE_TABLE_FREE_LIST
{
    struct _EX_PUSH_LOCK FreeListLock;                                      //0x0
    union HANDLE_TABLE_ENTRY* FirstFreeHandleEntry;                        //0x8
    union HANDLE_TABLE_ENTRY* LastFreeHandleEntry;                         //0x10
    LONG HandleCount;                                                       //0x18
    ULONG HighWaterMark;                                                    //0x1c
};

//0x8 bytes (sizeof)
struct _EXHANDLE
{
    union
    {
        struct
        {
            ULONG TagBits : 2;                                                //0x0
            ULONG Index : 30;                                                 //0x0
        };
        VOID* GenericHandleOverlay;                                         //0x0
        ULONGLONG Value;                                                    //0x0
    };
};

//0x80 bytes (sizeof)
typedef struct _HANDLE_TABLE_10
{
    ULONG NextHandleNeedingPool;                                            //0x0
    LONG ExtraInfoPages;                                                    //0x4
    volatile ULONGLONG TableCode;                                           //0x8
    struct _EPROCESS* QuotaProcess;                                         //0x10
    struct _LIST_ENTRY HandleTableList;                                     //0x18
    ULONG UniqueProcessId;                                                  //0x28
    union
    {
        ULONG Flags;                                                        //0x2c
        struct
        {
            UCHAR StrictFIFO : 1;                                             //0x2c
            UCHAR EnableHandleExceptions : 1;                                 //0x2c
            UCHAR Rundown : 1;                                                //0x2c
            UCHAR Duplicated : 1;                                             //0x2c
            UCHAR RaiseUMExceptionOnInvalidHandleClose : 1;                   //0x2c
        };
    };
    struct _EX_PUSH_LOCK HandleContentionEvent;                             //0x30
    struct _EX_PUSH_LOCK HandleTableLock;                                   //0x38
    union
    {
        struct _HANDLE_TABLE_FREE_LIST FreeLists[1];                        //0x40
        struct
        {
            UCHAR ActualEntry[32];                                          //0x40
            struct _HANDLE_TRACE_DEBUG_INFO* DebugInfo;                     //0x60
        };
    };
}HANDLE_TABLE_10, * PHANDLE_TABLE_10;

//0x10 bytes (sizeof)
typedef struct _HANDLE_TABLE_ENTRY_10
{
    volatile LONGLONG VolatileLowValue;                                     //0x0
    LONGLONG LowValue;                                                      //0x0
    struct
    {
        struct _HANDLE_TABLE_ENTRY_INFO* volatile InfoTable;                //0x0
        LONGLONG HighValue;                                                     //0x8
        union HANDLE_TABLE_ENTRY* NextFreeHandleEntry;                         //0x8
        struct _EXHANDLE LeafHandleValue;                                   //0x8
    };
    LONGLONG RefCountField;                                                 //0x0
    ULONGLONG Unlocked : 1;                                                   //0x0
    ULONGLONG RefCnt : 16;                                                    //0x0
    ULONGLONG Attributes : 3;                                                 //0x0
    struct
    {
        ULONGLONG ObjectPointerBits : 44;                                     //0x0
        ULONG GrantedAccessBits : 25;                                             //0x8
        ULONG NoRightsUpgrade : 1;                                                //0x8
        ULONG Spare1 : 6;                                                     //0x8
    };
    ULONG Spare2;                                                           //0xc
}HANDLE_TABLE_ENTRY_10, * PHANDLE_TABLE_ENTRY_10;

//0x40 bytes (sizeof)
struct _OBJECT_CREATE_INFORMATION
{
    ULONG Attributes;                                                       //0x0
    VOID* RootDirectory;                                                    //0x8
    CHAR ProbeMode;                                                         //0x10
    ULONG PagedPoolCharge;                                                  //0x14
    ULONG NonPagedPoolCharge;                                               //0x18
    ULONG SecurityDescriptorCharge;                                         //0x1c
    VOID* SecurityDescriptor;                                               //0x20
    struct _SECURITY_QUALITY_OF_SERVICE* SecurityQos;                       //0x28
    struct _SECURITY_QUALITY_OF_SERVICE SecurityQualityOfService;           //0x30
}; 

//0x38 bytes (sizeof)
typedef struct _OBJECT_HEADER
{
    LONGLONG PointerCount;                                                  //0x0
    union
    {
        LONGLONG HandleCount;                                               //0x8
        VOID* NextToFree;                                                   //0x8
    };
    struct _EX_PUSH_LOCK Lock;                                              //0x10
    UCHAR TypeIndex;                                                        //0x18
    UCHAR TraceFlags;                                                       //0x19
    UCHAR InfoMask;                                                         //0x1a
    UCHAR Flags;                                                            //0x1b
    union
    {
        struct _OBJECT_CREATE_INFORMATION* ObjectCreateInfo;                //0x20
        VOID* QuotaBlockCharged;                                            //0x20
    };
    VOID* SecurityDescriptor;                                               //0x28
    struct _QUAD Body;                                                      //0x30
}OBJECT_HEADER, * POBJECT_HEADER;

//0xd0 bytes (sizeof)
typedef struct _OBJECT_TYPE
{
    struct _LIST_ENTRY TypeList;                                            //0x0
    struct _UNICODE_STRING Name;                                            //0x10
    VOID* DefaultObject;                                                    //0x20
    UCHAR Index;                                                            //0x28
    ULONG TotalNumberOfObjects;                                             //0x2c
    ULONG TotalNumberOfHandles;                                             //0x30
    ULONG HighWaterNumberOfObjects;                                         //0x34
    ULONG HighWaterNumberOfHandles;                                         //0x38
    char TypeInfo[0x70];                                                    //0x40
    struct _EX_PUSH_LOCK TypeLock;                                          //0xb0
    ULONG Key;                                                              //0xb8
    struct _LIST_ENTRY CallbackList;                                        //0xc0
}OBJECT_TYPE, * POBJECT_TYPE;

void PspCidTableWalker(PULONG_PTR lpPspCidTable);

void ProcessTableWalker(ULONG uId);
